Skip to content

Small optimization reducing repeated int.ToString calls in completion#82765

Merged
ToddGrun merged 2 commits intodotnet:mainfrom
ToddGrun:dev/toddgrun/SmallCompletionContextPositionCache
Mar 17, 2026
Merged

Small optimization reducing repeated int.ToString calls in completion#82765
ToddGrun merged 2 commits intodotnet:mainfrom
ToddGrun:dev/toddgrun/SmallCompletionContextPositionCache

Conversation

@ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Mar 15, 2026

SymbolCompletionItem.CreateWorker can be called thousands of times for each completion list, and in the scenarios I've tested, it passes in the same contextPosition for each call. We need this context position as a string to store in a properties collection, so previously the code always ToString'ed the position. This PR creates a simple single item cache that is used to commonly avoid this allocation.

Not a huge win, but it's 0.2% of total allocations in the trace I'm looking at.

image ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com//pull/82765)

SymbolCompletionItem.CreateWorker can be called thousands of times for each completion list, and in the scenarios I've tested, it passes in the same contextPosition for each call. We need this context position as a string to store in a properties collection, so previously the code always ToString'ed the position. This PR creates a simple single item cache that is used to commonly avoid this allocation.

Not a huge win, but it's 0.2% of total allocations in the trace I'm looking at.
@ToddGrun ToddGrun requested a review from a team as a code owner March 15, 2026 01:51
@ToddGrun
Copy link
Contributor Author

@dotnet/roslyn-ide ptal

Copy link
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit hard to tell whether this ContextPosition is really needed much in the first place -- I can't entirely tell if this is the location of the symbol, or just being used for figuring out quick info later. But the change here is simple enough.

@ToddGrun ToddGrun merged commit 491aa49 into dotnet:main Mar 17, 2026
25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants